<template>
  <a-modal
    :maskClosable="false"
    :destroyOnClose="true"
    :footer="null"
    title="问卷调查结果"
    centered
    width="1200px"
    v-model="visible"
    :bodyStyle="{paddingTop: '10px'}"
  >
    <!-- 审核表格 -->
    <a-button type="primary" class="_mb-10" @click="exportExcel">导出</a-button>
    <s-table size="default" ref="auditTable" rowKey="id" :columns="columns" :data="loadData" :scroll="{ x: 1150, }">
      <template slot="action" slot-scope="text, record, index">
        <template v-if="text && Array.isArray(text)">
          <img v-for="img in text" :src="img" style="width: 50px;height:50px;margin-left: 10px;" />
        </template>
        <span v-else>{{ text }}</span>
      </template>
    </s-table>
  </a-modal>
</template>

<script>
import { STable } from '@/components'
import { resultList, resultTitleList, exportList } from '@/api/operations/questionnaire'

export default {
  components: {
    STable
  },
  data() {
    return {
      visible: false,
      // 查询参数
      questionnaireId: '',
      // 审核表单
      columns: [],
      pagination: {
        current: 1,
        pageSize: 10
      },
      loadData: async parameter => {
        let res = await resultList(Object.assign(parameter, { id: this.questionnaireId }))
        let records = (res.data.records || []).map(item => {
          let obj = {}
          Object.keys(item).forEach(k => {
            if (item[k].toString().indexOf('[{') != -1) {
              if (item[k].toString().indexOf('img') != -1) {
                obj[k] = (JSON.parse(item[k]) || []).map(m => m.img)
                // console.log('obj[k]--->', obj[k])
              } else {
                obj[k] = (JSON.parse(item[k]) || []).map(m => m.words || m.text).join(',')
              }
            } else {
              obj[k] = item[k]
            }
          })
          return obj
        })
        res.data.records = records
        return res
        // return resultList(Object.assign(parameter, { id: this.questionnaireId }))
      },
      selectedRowKeys: []
    }
  },
  watch: {
    visible(val) {
      if (val) this.getResultTitleList()
    }
  },
  methods: {
    exportExcel() {
      exportList({ id: this.questionnaireId })
    },
    getResultTitleList() {
      resultTitleList(this.questionnaireId).then(res => {
        this.columns = (res.data || []).map(item => {
          return {
            ...item,
            scopedSlots: { customRender: 'action' },
            ellipsis: true,
            width: 200
          }
        })
        // JSON.stringify(this.columns)
      })
    },
    moduleShow(id) {
      this.questionnaireId = id
      this.visible = true
    },
    // 审核列表多选
    onSelectChange(selectedRowKeys) {
      this.selectedRowKeys = selectedRowKeys
    },
    // 审核状态
    audits(id, status) {
      var _this = this
      let statuss = 3
      if (status == 1) {
        this.$confirm({
          title: '确认审核通过?',
          onOk() {
            activityAudits({ status: statuss, id: id }).then(data => {
              _this.handleReload()
            })
          }
        })
      }
    },
    // 审核不通过
    auditsChange(id, status) {
      var _this = this
      let statuss = 2
      if (status == 1) {
        this.$confirm({
          title: '确认不通过审核?',
          onOk() {
            activityAudits({ status: statuss, id: id }).then(data => {
              _this.handleReload()
            })
          }
        })
      }
    },

    // 批量审核通过
    batchAudits(id, status) {
      var _this = this
      let statuss = 3
      this.$confirm({
        title: '确认批量审核通过?',
        onOk() {
          activityAudits({ id: id.join(','), status: statuss }).then(res => {
            if (res.code === 0) {
              _this.$message.success(`审核成`)
              _this.handleOk()
            } else {
              _this.$message.error(res.msg)
            }
            _this.selectedRowKeys = []
          })
        },
        onCancel() {}
      })
    },

    handleOk() {
      this.$refs.auditTable.refresh(true)
    },
    handleReload() {
      this.$refs.auditTable.refresh()
    }
  }
}
</script>

<style></style>
